from bs4 import BeautifulSoup
import requests
import xlwt

# 中医大宝库数据爬取
base_url = 'https://www.zydbk.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
wb = xlwt.Workbook()


def findTitle(title):
    start = title.find('【')
    end = title.find('】')
    if start != -1 and end != -1:
        return title[start: end]
    else:
        return ''


def findContent(data):
    start = data.find('【')
    end = data.find('】')
    if start != -1 and end != -1:
        return data[end: len(data)]
    else:
        return ''


def writeTextToItem(keywords, result_txt, item):
    switcher = {
        '【方剂名】': 'fjm',
        '【出处】': 'cc',
        '【类别】': 'lb',
        '【组成】': 'zc',
        '【用法】': 'yf',
        '【功效】': 'gx',
        '【主治】': 'zz',
        '【方歌】': 'fg',
        '【实验】': 'sy',
        '【方解】': 'fj',
        '【加减】': 'jiaj',
        '【辩证要点】': 'bzyd',
        '【禁忌】': 'jinj',
        '【选方】': 'xf',
        '【分类】': 'lb',
        '用法': 'yf',
        '主治': 'zz',
        '加减': 'jiaj',
        '方解': 'fj',
        '方歌': 'fg',
        '禁忌': 'jinj',
        '方剂名': 'fjm',
        '方剂': 'fjm'
    }
    for k, v in switcher.items():
        if k in keywords:
            position = result_txt.find('】')
            text = ''
            if position != -1:
                text = result_txt[position + 1:len(result_txt)]
            else:
                text = result_txt.replace(k, '').replace('【】', '')
            if text != '':
                item[v] = text


def start():
    style = xlwt.XFStyle()
    style.alignment.wrap = 1
    # 表格头部style
    style1 = xlwt.XFStyle()
    font = xlwt.Font()
    font.bold = True
    font.height = 150
    font.colour_index = 0x0C
    al = xlwt.Alignment()
    al.horz = 0x02  # 设置水平居中
    al.vert = 0x01  # 设置垂直居中
    pattern = xlwt.Pattern()  # Create the Pattern
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN  # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    pattern.pattern_fore_colour = 5
    style1.alignment = al
    style1.font = font
    style1.pattern = pattern
    # 获取方剂类型
    response = requests.get(base_url + '/fangji/', headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    typeLabels = soup.find('div', class_='nav-s').findAll('a', class_='radius-rounded')
    types = []
    totalSize = 0
    for label in typeLabels:
        if label.text != '全部':
            types.append({
                'text': label.text,
                'href': label['data-url']
            })

    for type in types:
        # for i in range(0, 1):
        sh = wb.add_sheet(type['text'], cell_overwrite_ok=True)
        sh.write(0, 0, '方剂名', style1)
        sh.write(0, 1, '出处', style1)
        sh.write(0, 2, '类别', style1)
        sh.write(0, 3, '组成', style1)
        sh.write(0, 4, '用法', style1)
        sh.write(0, 5, '功效', style1)
        sh.write(0, 6, '主治', style1)
        sh.write(0, 7, '方歌', style1)
        sh.write(0, 8, '实验', style1)
        sh.write(0, 9, '方解', style1)
        sh.write(0, 10, '加减', style1)
        sh.write(0, 11, '辩证要点', style1)
        sh.write(0, 12, '禁忌', style1)
        sh.write(0, 13, '选方', style1)

        sh.col(0).width = 256 * 40
        sh.col(1).width = 256 * 40
        sh.col(2).width = 256 * 40
        sh.col(3).width = 256 * 40
        sh.col(4).width = 256 * 40
        sh.col(5).width = 256 * 40
        sh.col(6).width = 256 * 40
        sh.col(7).width = 256 * 40
        sh.col(8).width = 256 * 40
        sh.col(9).width = 256 * 40
        sh.col(10).width = 256 * 40
        sh.col(11).width = 256 * 40
        sh.col(12).width = 256 * 40
        sh.col(13).width = 256 * 40
        response1 = requests.get(base_url + type['href'], headers=headers)
        soup1 = BeautifulSoup(response1.content, 'html.parser')
        outResult = []
        # 该分类下所有药方
        prescripts = soup1.find('div', class_='s_tags mt16').findAll('a')
        for prescript in prescripts:
            response2 = requests.get(base_url + prescript['href'], headers=headers)
            soup2 = BeautifulSoup(response2.content, 'html.parser')
            p_list = soup2.find('div', class_='info-con').findAll('p')
            result_item = {
                'fjm': '',
                'cc': '',
                'lb': '',
                'zc': '',
                'yf': '',
                'gx': '',
                'zz': '',
                'fg': '',
                'sy': '',
                'fj': '',
                'jiaj': '',
                'bzyd': '',
                'jinj': '',
                'xf': ''
            }
            for p in p_list:
                result_item['fjm'] = soup2.find('div', class_='info-title').find('h1').text
                strongs = p.findAll('strong')
                if len(strongs) == 1:
                    text = strongs[0].text
                    writeTextToItem(text, p.text, result_item)
                else:
                    writeTextToItem(findTitle(p.text), p.text, result_item)
            print(result_item)
            outResult.append(result_item)
            for i in range(0, len(outResult)):
                try:
                    data = outResult[i]
                    sh.write(i + 1, 0, data['fjm'], style)
                    sh.write(i + 1, 1, data['cc'], style)
                    sh.write(i + 1, 2, data['lb'], style)
                    sh.write(i + 1, 3, data['zc'], style)
                    sh.write(i + 1, 4, data['yf'], style)
                    sh.write(i + 1, 5, data['gx'], style)
                    sh.write(i + 1, 6, data['zz'], style)
                    sh.write(i + 1, 7, data['fg'], style)
                    sh.write(i + 1, 8, data['sy'], style)
                    sh.write(i + 1, 9, data['fj'], style)
                    sh.write(i + 1, 10, data['jiaj'], style)
                    sh.write(i + 1, 11, data['bzyd'], style)
                    sh.write(i + 1, 12, data['jinj'], style)
                    sh.write(i + 1, 13, data['xf'], style)
                except Exception as e:
                    print(f"发生异常{e}")
            totalSize += len(outResult)

    wb.save('./爬取的数据/中医大宝库.xls')
    print(f'处理完毕,共{totalSize}条方剂')


start()
